<template>
  <div class="profile-page">
    <div class="profile-content">
      <!-- 个人信息卡片 -->
      <div class="info-card">
        <div class="card-header">
          <h3>基本信息</h3>
        </div>
        
        <div class="info-content">
          <div class="info-item">
            <span class="label">用户名：</span>
            <span class="value">{{ userInfo?.username || '-' }}</span>
          </div>
          <div class="info-item">
            <span class="label">邮箱：</span>
            <span class="value">{{ userInfo?.email || '-' }}</span>
          </div>
          <div class="info-item">
            <span class="label">手机号：</span>
            <span class="value">{{ userInfo?.phone || '-' }}</span>
          </div>
          <div class="info-item">
            <span class="label">注册时间：</span>
            <span class="value">{{ formatDate(userInfo?.registerDate || userInfo?.createdAt) || '-' }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { computed, onMounted } from 'vue'
import { useStore } from 'vuex'

export default {
  name: 'UserProfile',
  setup() {
    const store = useStore()
    
    // 计算属性
    const userInfo = computed(() => store.getters.getUserInfo)
    
    // 方法
    const formatDate = (dateString) => {
      if (!dateString) return ''
      const date = new Date(dateString)
      return date.toLocaleString('zh-CN')
    }
    
    // 获取用户信息
    const fetchUserInfo = async () => {
      try {
        await store.dispatch('fetchUserProfile')
      } catch (error) {
        console.error('获取用户信息失败:', error)
      }
    }
    
    // 页面加载时获取用户信息
    onMounted(() => {
      fetchUserInfo()
    })
    
    return {
      userInfo,
      formatDate
    }
  }
}
</script>

<style scoped>
.profile-page {
  padding: 24px;
}

.profile-content {
  display: grid;
  gap: 24px;
}

/* 信息卡片 */
.info-card {
  background: #f8f9ff;
  border-radius: 12px;
  padding: 24px;
  border: 1px solid #e4e7ed;
}

.card-header {
  margin-bottom: 20px;
}

.card-header h3 {
  margin: 0;
  font-size: 18px;
  color: #303133;
}

.info-content {
  display: grid;
  gap: 16px;
}

.info-item {
  display: flex;
  align-items: center;
}

.info-item .label {
  width: 100px;
  color: #606266;
  font-weight: 500;
}

.info-item .value {
  color: #303133;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .info-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }
  
  .info-item .label {
    width: auto;
  }
}
</style> 